/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "sle_connect_service.h"
#include "sle_connect_manager.h"
#include "sle_adapter_manager.h"
#include "cmd_input.h"
#include <vector>
#include <memory>
#include <cstring>
#include <iterator>

namespace OHOS {
namespace NearLink {
namespace SleStandard {
namespace {
using namespace testing::ext;
SleConnectService *connService_ = nullptr;
SleConnectManager *connManager_ = nullptr;
SleAdapterManager *adapterManager_ = nullptr;
} // namespace

class ConnectHandlerTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void ConnectHandlerTest::SetUpTestCase() {}

void ConnectHandlerTest::TearDownTestCase() {}

void ConnectHandlerTest::SetUp() {
    connService_ = SleConnectService::GetInstance();
    connManager_ = SleConnectManager::GetInstance();
    adapterManager_ = SleAdapterManager::GetInstance();
}

void ConnectHandlerTest::TearDown() {}

HWTEST_F(ConnectHandlerTest, ConnectTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addrStr[18]; // 重命名为 addrStr
    strcpy(addrStr, "00:11:aa:bb:f1:6b");
    if(strlen(addrStr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addrStr);
    SleAddress sleAddr(remoteAddr); // 修改为 sleAddr
    SleErrorCode ret = connManager_->Connect(sleAddr);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, DisconnectTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    if(strlen(addr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addr);
    SleAddress address(remoteAddr);
    SleErrorCode ret = connManager_->Disconnect(address);
    EXPECT_EQ(ret, SLE_ERR_DISCONNECT_SUCCESS_WHEN_DISCONNECTED);
}

HWTEST_F(ConnectHandlerTest, GetConnectIdTest, TestSize.Level1)
{
    uint16_t connectId;
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    if(strlen(addr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addr);
    SleAddress address(remoteAddr);
    SleErrorCode ret = connManager_->GetConnectId(address, connectId);
    EXPECT_EQ(ret, 0);
}

HWTEST_F(ConnectHandlerTest, GetConnectStateTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    if(strlen(addr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addr);
    SleAddress address(remoteAddr);
    int32_t connState;
    SleErrorCode ret = connManager_->GetConnectState(address, connState);
    EXPECT_EQ(ret, 0);
}

HWTEST_F(ConnectHandlerTest, GetConnectDevListTest, TestSize.Level1)
{
    uint16_t number;
    std::vector<SleConnectDevice> connDevices;
    SleErrorCode ret = connManager_->GetConnectDeviceList(connDevices, number);
    EXPECT_EQ(ret, 0);
}

// HWTEST_F(ConnectHandlerTest, SetDataBufferLengthTest, TestSize.Level1)
// {
//     uint16_t connectId = 255;
//     uint16_t length = 10;
//     SleErrorCode ret = connService_->SetDataBufferLength(connectId, length);
//     EXPECT_EQ(ret, 0);
// }

HWTEST_F(ConnectHandlerTest, UpdateConnParamTest, TestSize.Level1)
{
    uint16_t connectId = 255;
    // SleConnectionPhyParam phy_param = {0};
    // phy_param.tx_phy = 2;
    // phy_param.rx_phy = 2;
    // phy_param.tx_format = 1;
    // phy_param.rx_format = 1;
    // phy_param.tx_pilot_density = 2;
    // phy_param.rx_pilot_density = 2;
    // phy_param.g_feedback = 0;
    // phy_param.t_feedback = 0;
    // SleErrorCode ret = SetPhyParam(connectId, phy_param);
    // uint8_t mcs = 10;
    // ret = SetMcsValue(connectId, mcs);

    SleConnectParam param;
    param.intervalMin = 255;
    param.intervalMax = 255;
    param.latency = 255;
    param.supervisionTimeout = 10;
    SleConnectionParam params(param);
    SleErrorCode ret = connManager_->UpdateConnectParam(connectId, params);
    EXPECT_EQ(ret, SLE_ERR_NOT_READY);
}

HWTEST_F(ConnectHandlerTest, SendPairReqTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    if(strlen(addr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addr);
    SleAddress address(remoteAddr);
    SleErrorCode ret = connService_->SendPairRequest(address);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, RemovePairTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    if(strlen(addr) != 17) {
        printf("[SleConnectTestTool] SleSetHostAddress failed, invalid addr\r\n");
        return;
    }
    Str2Mac(remoteAddr.addr, addr);
    SleAddress address(remoteAddr);
    SleErrorCode ret = connService_->RemovePairedDevice(address);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, RemoveAllPairsTest, TestSize.Level1)
{
    SleErrorCode ret = connService_->RemoveAllPairedDevices();
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, GetPairedDevicesNum, TestSize.Level1)
{
    uint16_t number;
    SleErrorCode ret = connService_->GetPairedDevicesNum(number);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, GetPairStateTest, TestSize.Level1)
{
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18];
    strcpy(addr, "00:11:aa:bb:f1:6b");
    Str2Mac(remoteAddr.addr, addr);
    // SlePairState pairState;
    uint8_t state;
    SleAddress address(remoteAddr);
    SleErrorCode ret = connService_->GetPairState(address, state);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(ConnectHandlerTest, ReadRemoteRssiTest, TestSize.Level1)
{
    uint16_t connectId = 255;
    SleErrorCode ret = connService_->ReadRemoteRssi(connectId);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}
}
} // namespace NetManagerStandard
} // namespace OHOS
